package com.schoolhelper.controller.activity;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.hyphenate.easeui.EaseIM;
import com.hyphenate.easeui.constants.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.modules.chat.EaseChatFragment;
import com.hyphenate.easeui.provider.EaseUserProfileProvider;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.schoolhelper.controller.fragment.ChatFragment;
import com.schoolhelper.model.dao.okhttp.LwlRequestUrl;
import com.schoolhelper.model.entity.User;
import com.schoolhelper.utils.ImageUtils;
import com.schoolhelper.utils.constant.LwlApi;

import java.util.HashMap;
import java.util.Map;

public class ChatActivity extends FragmentActivity {

    private String mHxid;
    private ChatFragment chatFragment;
    private LocalBroadcastManager mLBM;
    private int mChatType;

    private EaseTitleBar chatTitleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        
        initData();

        initListener();
    }

    private void initListener() {
        chatTitleBar.setOnBackPressListener(v -> {
            finish();
        });
    }

    private void initData() {
        // 创建一个会话的fragment
        chatFragment = new ChatFragment();

        mHxid = getIntent().getStringExtra(EaseConstant.EXTRA_CONVERSATION_ID);

        // 获取聊天类型
        mChatType = getIntent().getExtras().getInt(EaseConstant.EXTRA_CHAT_TYPE);

        chatFragment.setArguments(getIntent().getExtras());

        // 替换fragment
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_chat, chatFragment).commit();

        // 获取发送广播的管理者
        mLBM = LocalBroadcastManager.getInstance(ChatActivity.this);

        chatTitleBar = findViewById(R.id.chat_title_bar);
        chatTitleBar.setTitle(mHxid);

        EaseIM.getInstance().setUserProvider(new EaseUserProfileProvider() {
            @Override
            public EaseUser getUser(String username) {
                Map<String, String> headers = new HashMap<>();
                Map<String, String> requestBody = new HashMap<>();
                requestBody.put("phone", username);
                String postResponseJson = LwlRequestUrl.getPostResponseJson(LwlApi.BASE_URL, LwlApi.GET_BY_PHONE, headers, requestBody);
                System.out.println("查询结果：" + postResponseJson);
                try {
                    User user = JSONObject.parseObject(postResponseJson, User.class);
                    EaseUser easeUser = new EaseUser(username);
                    easeUser.setNickname(user.getUsername());
                    easeUser.setAvatar(user.getProfileImg());
                    return easeUser;
                } catch (Exception e) {
                    runOnUiThread(() -> {
                        Toast.makeText(getApplicationContext(), "查询失败", Toast.LENGTH_SHORT).show();
                    });
                    e.printStackTrace();
                    return null;
                }
            }
        });
    }
}